<?php
/**
 * 表单类模型
 *
 *@author hary <hary@taomee.com>
 */
class file_model extends model
{
	private $file_table;

	/**
	 * @return max
	 */
	function __construct()
	{
		parent::__construct();

		$this->file_table   = 'file';
	}

	/**
	 * 添加上传文件
	 * @param $banner
	 * @return max
	 */
	public function uploadFile(){
	
		$this->loadModel("upload_model");

		$ret = $this->upload_model->upload('upload_file','upload');
		
		$file['f_type']  	= $ret['type'];
		$file['f_width'] 	= $ret['width'];
		$file['f_height'] 	= $ret['height'];
		$file['f_px'] 		= $ret['width'].' x '.$ret['height'];
		$file['f_name']  	= $ret['name'];
		$file['f_url']		= $ret['url'];
		$file['f_path']		= $ret['path'];
		$file['f_update_time'] = time();

		$this->addDataBatch2($file,array('f_name','f_px','f_width','f_height','f_url','f_path','f_type','f_update_time','sort'));

		$result = $this->dataInsert($this->file_table);

		if($result) $file['f_id'] = $this->db_insert_id;

		return $result?$file:$result;
	}

	/**
	 * 获取文件
	 * @return max
	 */
	public function getFiles($keywords,$page='1',$page_size='6',$order='sort desc'){
		
		$keywords = $this->formatData($keywords);
		
		if(empty($keywords))
		{
			$sql = "select * from s_$this->file_table ";
		}
		else
		{
			$sql = "select * from s_$this->file_table where
   				f_name like '%$keywords%' or 
   				f_des like '%$keywords%'";
		}
			
		if($page) $this->setPage($page);
			
		if($page_size) $this->setLimit($page_size);
			
		if($order) $this->setOrder('order by '.$order);
			
		return $this->customQuery($sql);
	}

	/**
	 * 根据id删除file
	 * @param $fid
	 * @return boolean
	 */
	public function delFileById($fid){
		
		$fid = intval($fid);
		
		$file = $this->getFileById($fid);
		
		@unlink($file['f_path']);
		
		$this->where("f_id",$fid,'=','and',0);
		
		return $this->dataDel($this->file_table);
	}

	/**
	 * 根据id获取file
	 * @param $fid
	 * @return array
	 */
	public function getFileById($fid)
	{
		$fid = intval($fid);

		$this->where("f_id",$fid,'=','and',0);

		return $this->getDataInfo($this->file_table);
	}

}